package com.chuyu.cy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuyu.cy.contants.ResponseCodeEnum;
import com.chuyu.cy.entity.domain.Tag;
import com.chuyu.cy.entity.dto.TagDTO;
import com.chuyu.cy.entity.vo.TagVO;
import com.chuyu.cy.exception.CustomException;
import com.chuyu.cy.mapper.TagMapper;
import com.chuyu.cy.service.TagService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author 周小白
* @description 针对表【tag】的数据库操作Service实现
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
    implements TagService{


    @Resource
    private TagMapper tagMapper;



    /**
     * 获取标签列表
     * @return 标签列表
     */
    @Override
    public List<TagVO> getTagList() {
        List<Tag> tags = tagMapper.selectList(null);

        if (tags == null || tags.isEmpty()) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        List<TagVO> tagVOList = new ArrayList<>();

        tags.forEach(tag -> {
            TagVO tagVO = new TagVO();
            tagVO.setId(tag.getId());
            tagVO.setTagName(tag.getTagName());

            tagVOList.add(tagVO);
        });

        return tagVOList;
    }




    /**
     * 插入标签
     * @param tagDTO 标签
     * @return 插入结果
     */
    @Override
    public boolean insertTag(TagDTO tagDTO) {
        if (tagDTO == null) {
            throw new CustomException(ResponseCodeEnum.ERROR_PARAM);
        }
        if (StringUtils.isEmpty(tagDTO.getTagName())) {
            throw new CustomException(ResponseCodeEnum.ERROR_PARAM);
        }

        Tag tag = new Tag();
        tag.setTagName(tagDTO.getTagName());

        int insert = tagMapper.insert(tag);

        if (insert <= 0) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return true;
    }


}




